package com.library.admin.form;

import books.borrowing.entity.SubscriberEntity;

import com.library.admin.listener.UpdateOwnAccountListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;

public class UpdateOwnAccountForm extends Form {
	private static final long serialVersionUID = -8032217885473149142L;

	private SubscriberEntity administrator;
	private TextField nameTextField;
	private TextField surnameTextField;
	private TextField loginTextField;
	private PasswordField passwordTextField;
	private Button submitButton;
	
	public UpdateOwnAccountForm(SubscriberEntity administrator) {
		this.administrator = administrator;
		init();
	}

	private void init() {
		setWidth("100");
		setImmediate(true);
		
		nameTextField = new TextField("Prenom");
		nameTextField.setValue(administrator.getName());
		surnameTextField = new TextField("Nom");
		surnameTextField.setValue(administrator.getForename());
		loginTextField = new TextField("Login");
		loginTextField.setValue(administrator.getLogin());
		passwordTextField = new PasswordField("Mot de passe");
		passwordTextField.setValue(administrator.getPassword());
		submitButton = new Button("Sauvegarder");
		submitButton.addListener(new UpdateOwnAccountListener(administrator, this));
		
		HorizontalLayout buttonLayout = new HorizontalLayout();
		buttonLayout.addComponent(submitButton);
		buttonLayout.setComponentAlignment(submitButton, Alignment.MIDDLE_CENTER);
		setFooter(buttonLayout);
		
		addField("nameTextField", nameTextField);
		addField("surnameTextField", surnameTextField);
		addField("loginTextField", loginTextField);
		
		getField("nameTextField").setRequired(true);
		getField("nameTextField").setRequiredError("Le champ \"Prenom\" est obligatoire");
		getField("surnameTextField").setRequired(true);
		getField("surnameTextField").setRequiredError("Le champ \"Nom\" est obligatoire");
		getField("loginTextField").setRequired(true);
		getField("loginTextField").setRequiredError("Le champ \"Login\" est obligatoire");
	}

	public TextField getNameTextField() {
		return nameTextField;
	}

	public TextField getSurnameTextField() {
		return surnameTextField;
	}

	public TextField getLoginTextField() {
		return loginTextField;
	}
}
